<template>
	<view class="content">
		<view class="menu_box delivery_box">
			<view class="" v-if="nowBusinessInfo.logisticsType.noDelivery == 1">
				<u-radio-group v-model="logisticsType" placement="row" @change="selectOrderTap()">
					<u-radio
						:activeColor="templateConfig.page.themeColor"
						:name="7"
						label="商品无需物流"
						shape="square"
					></u-radio>
				</u-radio-group>
			</view>

			<u-radio-group v-model="logisticsType" placement="row" @change="selectOrderTap()" v-else>
				<u-radio :activeColor="templateConfig.page.themeColor" :name="2" label="快递" shape="square"></u-radio>
				<u-radio
					v-if="nowBusinessInfo.logisticsType.oneselfDelivery == 1"
					:activeColor="templateConfig.page.themeColor"
					:name="3"
					label="自提"
					shape="square"
				></u-radio>
				<u-radio
					v-if="nowBusinessInfo.payType.cashDelivery == 1"
					:activeColor="templateConfig.page.themeColor"
					:name="6"
					label="货到付款"
					shape="square"
				></u-radio>
			</u-radio-group>
		</view>

		<view class="" v-if="nowBusinessInfo.logisticsType.noDelivery == 0">
			<view class="menu_box" v-if="logisticsType == 1 || logisticsType == 2">
				<view class="address_box" @click="getPathTap('/packageA/address/addressList?type=select')">
					<view class="address" v-if="JSON.stringify(addressData) !== '{}'">
						<view class="icone">
							<view
								class="iconfont icon-shouhuodizhi"
								style="font-size: 50rpx"
								:style="{ color: templateConfig.page.themeColor }"
							></view>
						</view>
						<view class="left">
							<view class="txt">
								{{ addressData.province }}{{ addressData.city }}{{ addressData.district
								}}{{ addressData.address }}{{ addressData.houseNumber }}
							</view>
							<view class="tit">
								{{ addressData.name }}
								<span>{{ addressData.mobile }}</span>
							</view>
						</view>
						<view class="right">
							<view
								class="iconfont icon-bianzu"
								style="color: #999; font-size: 50rpx; margin-left: 10rpx"
							></view>
						</view>
					</view>

					<view v-else class="none_address">
						<aicon type="location" size="25"></aicon>
						<span>请选择收货地址</span>
					</view>
				</view>
			</view>
			<!-- 自提 -->
			<view v-if="logisticsType == 3" class="thatpickup">
				<view class="menu_box">
					<view class="phone_box" style="align-items: center">
						<view class="left">
							<u-icon name="phone" :color="templateConfig.page.themeColor" size="28"></u-icon>
						</view>
						<view class="input-box">
							<span>联系电话：</span>
							<view class="input">
								<input type="number" v-model="thataddress.mobile" placeholder="请输入联系电话" />
							</view>
						</view>
						<view class="btn" :style="{ background: templateConfig.page.themeColor }">
							获取当前号码
							<button open-type="getPhoneNumber" @getphonenumber="getBindWechatPhoneTap"></button>
						</view>
					</view>
					<view class="line"></view>
					<view class="phone_box">
						<view class="left">
							<u-icon name="pushpin" :color="templateConfig.page.themeColor" size="28"></u-icon>
						</view>
						<view class="thataddress">
							<view class="title">
								{{ nowBusinessInfo.businessName }}
							</view>
							<view class="name">
								{{ thataddress.address }}
							</view>
						</view>
						<view class="left" @click="getPathTap('/pages/index/company')">
							<view
								class="iconfont icon-bianzu"
								style="color: #999; font-size: 50rpx; margin-left: 10rpx"
							></view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="menu_box">
			<view class="goods" v-for="(item, index) in orderData" :key="index">
				<view class="left">
					<view class="" v-if="item.goodsSpecification.length > 0 && item.goodsSpecification[0] != null">
						<image
							v-if="item.goodsSpecification[0].specificationImages != ''"
							:src="item.goodsSpecification[0].specificationImages"
							lazy-load
						></image>
						<image v-else :src="item.plugsGoods.rotationImgsJson[0]" lazy-load mode="aspectFill"></image>
					</view>
					<view class="" v-else>
						<image :src="item.plugsGoods.rotationImgsJson[0]" mode="aspectFill" lazy-load></image>
					</view>
				</view>

				<view class="right">
					<text class="title" style="margin: 0">{{ item.plugsGoods.goodsName }}</text>
					<view class="guige" v-if="item.goodsSpecification.length > 0 && item.goodsSpecification[0] != null">
						{{
							item.goodsSpecification[0].specificationName
								.replace(/\"/g, '')
								.replace(/\{|}/g, '')
								.replace(/\,/g, '+')
						}}
					</view>
					<view class="guige" v-else>默认</view>
					<view class="price-box">
						<view
							class="price"
							v-if="item.plugsGoods.goodsType == 0"
							:style="{ color: templateConfig.page.themeColor, border }"
						>
							¥
							<text
								class=""
								v-if="item.goodsSpecification.length > 0 && item.goodsSpecification[0] != null"
							>
								{{ item.goodsSpecification[0].dealPrice }}
							</text>
							<text class="" v-else>{{ item.plugsGoods.goodsPrice }}</text>
						</view>

						<view
							class="price"
							v-if="item.plugsGoods.goodsType == 1"
							:style="{ color: templateConfig.page.themeColor, border }"
						>
							<text v-if="item.goodsSpecification.length > 0 && item.goodsSpecification[0] != null">
								{{ item.goodsSpecification[0].dealPrice }}积分
							</text>
							<text v-else>{{ item.plugsGoods.goodsPrice }}</text>
						</view>
						<text class="number">数量： {{ item.goodsNum }}</text>
					</view>
				</view>
			</view>
		</view>

		<view class="menu_box" v-if="goodsType == 0 && templateConfig.overall.onesState == 1">
			<view class="order_info_box">
				<view class="label-coupon">
					<view class="tit">优惠券</view>
					<view class="txt" @click="toggleMask('show')" v-if="couponList.length > 0">
						<text class="cell-tip active" v-if="JSON.stringify(selectCoupon) == '{}'">选择优惠券</text>
						<view class="cell-tip active" v-else>
							<view class="" v-if="selectCoupon.couponType == 0 || selectCoupon.couponType == 1">
								满{{ selectCoupon.plugsCoupons.howMuchFullMoney }}减{{
									selectCoupon.plugsCoupons.couponDenomination
								}}
							</view>
							<view class="" v-if="selectCoupon.couponType == 3">
								满{{ selectCoupon.plugsCoupons.howMuchFullMoney }}打{{
									selectCoupon.plugsCoupons.couponDenomination * 10
								}}折
							</view>
						</view>
					</view>
					<view class="txt" v-else>没有优惠券</view>
				</view>
			</view>
		</view>

		<view class="menu_box">
			<view class="order_info_box">
				<view class="label">
					<view class="tit">实际应付</view>
					<view class="txt" v-if="goodsType == 0">¥{{ JSON.parse(zongPrice) }}</view>
					<view class="txt" v-if="goodsType == 1">{{ JSON.parse(zongPrice) }}积分</view>
				</view>

				<view class="label" v-if="goodsType == 0">
					<view class="tit">商品合计</view>
					<view class="txt">¥{{ priceTotal }}</view>
				</view>
				<view class="label" v-if="goodsType == 1">
					<view class="tit">商品合计</view>
					<view class="txt">{{ priceTotal }}积分</view>
				</view>
				<view class="label" v-if="JSON.stringify(selectCoupon) != '{}'">
					<view class="tit">店铺优惠</view>
					<view class="txt" style="color: #f54319">减¥{{ couponPrice }}</view>
				</view>
				<view class="label" v-if="logisticsType == 2">
					<view class="tit">运费</view>
					<view class="txt">¥{{ distributionFee }}</view>
				</view>

				<!-- <view class="label" v-if="logisticsType == 3">
					<view class="tit">配送费</view>
					<view class="txt">¥{{ 0 }}</view>
				</view> -->
			</view>
		</view>
		<view class="menu_box" style="margin-bottom: 200rpx">
			<view class="order_info_box">
				<view class="label">
					<view class="tit">订单备注</view>
					<input
						type="text"
						v-model="ordermark"
						style="width: 76%; text-align: right; font-weight: 400"
						placeholder="订单备注"
					/>
				</view>
			</view>
		</view>

		<view class="footer">
			<view class="label">
				共{{ cartNums }}件，实付
				<text class="tex" :style="{ color: templateConfig.page.themeColor }" v-if="goodsType == 0">
					¥{{ JSON.parse(zongPrice) }}
				</text>
				<text class="tex" :style="{ color: templateConfig.page.themeColor }" v-if="goodsType == 1">
					{{ JSON.parse(zongPrice) }}积分
				</text>
				<view
					v-if="goodsType == 0"
					class="button"
					@click="paypalOrderTap()"
					style="backgroundcolor: #f54319"
					:style="{ background: templateConfig.page.themeColor }"
				>
					<span>提交订单</span>
				</view>
				<view
					v-if="goodsType == 1"
					class="button"
					@click="paypalOrderTapIntegral()"
					style="backgroundcolor: #f54319"
					:style="{ background: templateConfig.page.themeColor }"
				>
					<span>确认兑换</span>
				</view>
			</view>
		</view>

		<!-- 优惠券面板 -->
		<view class="mask" :class="maskState === 0 ? 'none' : maskState === 1 ? 'show' : ''" @click="toggleMask">
			<view class="mask-content">
				<!-- 优惠券页面，仿mt -->
				<view class="coupon-item" v-for="(item, index) in couponList" :key="index">
					<view class="" v-if="item.couponType != 2">
						<view class="" v-if="item.plugsCoupons.goodsType == 1">
							<view class="con">
								<view class="left">
									<text class="title">{{ item.couponName }}</text>
									<text class="time" v-if="item.plugsCoupons.goodsType == 1">全部商品可用</text>
									<text class="time" v-if="item.plugsCoupons.goodsType == 2">指定商品可用</text>
								</view>
								<view class="right">
									<text class="price" v-if="item.couponType == 0 || item.couponType == 1">
										￥{{ item.plugsCoupons.couponDenomination }}
									</text>
									<text class="price" v-if="item.couponType == 2">
										{{ item.plugsCoupons.couponDenomination }}分
									</text>
									<text class="price" v-if="item.couponType == 3">
										{{ item.plugsCoupons.couponDenomination * 10 }}折
									</text>
									<text v-if="item.couponType == 0 || item.couponType == 1 || item.couponType == 3">
										满{{ item.plugsCoupons.howMuchFullMoney }}可用
									</text>
								</view>

								<view class="circle l"></view>
								<view class="circle r"></view>
							</view>
							<view class="getlin" style="">
								<text class="tips" v-if="item.plugsCoupons.timeType == 1">
									{{ item.plugsCoupons.startTime }}--{{ item.plugsCoupons.endTime }}
								</text>
								<text class="tips" v-if="item.plugsCoupons.timeType == 2">
									有效期至领取内{{ item.plugsCoupons.timeDay }}天内有效
								</text>
								<view style="" v-if="filterCoupon(item) == 1" @click="useCoupon(item, index)">
									<view class="linqu" v-if="!item.checked">使用</view>
									<view class="linqu" v-if="item.checked" style="background-color: #f9ae3d">
										取消
									</view>
								</view>
								<view class="linqu" style="background-color: #999" v-else>不可用</view>
							</view>
						</view>
						<!-- 单品优惠 -->
						<view class="" v-if="item.plugsCoupons.goodsType == 2">
							<view class="con">
								<view class="left">
									<text class="title">{{ item.couponName }}</text>
									<text class="time" v-if="item.plugsCoupons.goodsType == 1">全部商品可用</text>
									<text class="time" v-if="item.plugsCoupons.goodsType == 2">指定商品可用</text>
								</view>
								<view class="right">
									<text class="price" v-if="item.couponType == 0 || item.couponType == 1">
										￥{{ item.plugsCoupons.couponDenomination }}
									</text>
									<text class="price" v-if="item.couponType == 2">
										{{ item.plugsCoupons.couponDenomination }}分
									</text>
									<text class="price" v-if="item.couponType == 3">
										{{ item.plugsCoupons.couponDenomination * 10 }}折
									</text>
									<text v-if="item.couponType == 0 || item.couponType == 1 || item.couponType == 3">
										满{{ item.plugsCoupons.howMuchFullMoney }}可用
									</text>
								</view>

								<view class="circle l"></view>
								<view class="circle r"></view>
							</view>
							<view class="getlin" style="">
								<text class="tips" v-if="item.plugsCoupons.timeType == 1">
									{{ item.plugsCoupons.startTime }}--{{ item.plugsCoupons.endTime }}
								</text>
								<text class="tips" v-if="item.plugsCoupons.timeType == 2">
									有效期至领取内{{ item.plugsCoupons.timeDay }}天内有效
								</text>
								<view style="" v-if="filterCoupon(item) == 1" @click="useCoupon(item, index)">
									<view class="linqu" v-if="!item.checked">使用</view>
									<view class="linqu" v-if="item.checked" style="background-color: #f9ae3d">
										取消
									</view>
								</view>
								<view class="linqu" style="background-color: #999" v-else>不可用</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import JSONBig from 'json-bigint';
import {
	searchUserInfoById,
	getShippingFeeEstimate,
	createOrder,
	searchPlugsCouponsByUserId,
	getUserSubmitFormDataList,
	getWeChartUserPhone,
	integralOrderPay,
	updateUserInfo
} from '../../static/utils/api.js';
export default {
	data() {
		return {
			templateConfig: uni.getStorageSync('templateConfig'),
			goodsType: 0,
			userInfo: uni.getStorageSync('userInfo'),
			businessInfo: {},
			priceTotal: 0,
			zongPrice: 0,
			couponPrice: 0,
			orderData: [],
			orderList: [],
			cartIdList: [],
			maskState: 0,
			num: 0,

			cardNumTotal: 0,
			couponsIndex: -1,
			couponsList: [],
			addressList: [], //地址列表
			addressData: {}, //当前地址
			addressIndex: '', // 地址下标
			orderInfo: '',
			goodsOrderList: [],
			userPhoneNumber: '',
			orderType: '',
			pintuanId: '',
			addCartInfo: [],
			list: [],
			payList: '',
			logisticsType: 2,
			goodsScoreStatus: false,
			integralInfo: {},
			time: '',
			userData: '',
			delivery: [
				{
					name: '快递',
					id: 2
				},
				{
					name: '自提',
					id: 3
				}
			],
			show2: false,
			cartListId: [], // 购物车ID列表
			couponList: [],
			useCouponId: '', // 使用优惠券ID
			selectCoupon: {}, // 选择优惠券的信息
			balanceMoney: 0, // 余额
			scoreType: '',
			integral: 0,
			appointMoney: 0,
			ordermark: '',
			distributionFee: 0,
			addressInfo: {},
			pickupTime: '',
			pickupTime: '',
			thataddress: {
				default: true,
				addressName: uni.getStorageSync('nowBusinessInfo').businessName || '',
				address: uni.getStorageSync('nowBusinessInfo').businessAddress.address || '',
				province: uni.getStorageSync('nowBusinessInfo').businessAddress.province || '',
				city: uni.getStorageSync('nowBusinessInfo').businessAddress.city || '',
				district: uni.getStorageSync('nowBusinessInfo').businessAddress.district || '',
				latitude: uni.getStorageSync('nowBusinessInfo').businessAddress.latitude || '',
				name: '',
				mobile: '',
				longitude: uni.getStorageSync('nowBusinessInfo').businessAddress.longitude || '',
				areaCode: '',
				houseNumber: ''
			},

			nowBusinessInfo: {
				logisticsType:{
					noDelivery:0
				}
			},
			cartNums: 0
		};
	},
	onShow() {
		let that = this;
		this.nowBusinessInfo = uni.getStorageSync('nowBusinessInfo');
		if (this.nowBusinessInfo.logisticsType.noDelivery == 1) {
			this.logisticsType = 7;
		}
		uni.$once('selectAddress', function (index) {
			console.log(index, 996);
			if (index != null) {
				let data = {
					userId: uni.getStorageSync('userInfo').id
				};
				searchUserInfoById(data).then((res) => {
					if (res.code == 200) {
						if (
							res.body.receiveAddressJson != '' &&
							res.body.receiveAddressJson != null &&
							res.body.receiveAddressJson != '[]'
						) {
							that.addressList = JSON.parse(res.body.receiveAddressJson);
							that.addressData = that.addressList[index];
							// that.getdisprice();
						}
					}
				});
			}
		});
		this.getcouponList();
	},
	onLoad(option) {
		this.orderData = uni.getStorageSync('shopCart');
		this.goodsType = this.orderData[0].plugsGoods.goodsType;
		this.orderData.forEach((item) => {
			console.log(Array.isArray(item.plugsGoods.rotationImgsJson), 6666);
			if (!Array.isArray(item.plugsGoods.rotationImgsJson) && item.plugsGoods.rotationImgsJson) {
				item.plugsGoods.rotationImgsJson = JSON.parse(item.plugsGoods.rotationImgsJson);
			}
		});
		// console.log(this.orderData, 665);
		let num = 0;
		this.orderData.forEach((item) => {
			let obj = {
				goodsId: item.goodsId,
				goodsNum: item.goodsNum,
				goodsName: item.plugsGoods.goodsName,
				goodsSpecificationIds: item.goodsSpecificationIds,
				goodsSpecificationNames:
					item.goodsSpecification.length > 0 ? item.goodsSpecification[0].specificationName : ''
			};
			num += item.goodsNum;
			this.orderList.push(obj);
			this.cartIdList.push(item.id);
		});
		this.cartNums = num;
		this.calcTotal();
		this.getuserInfo();
	},
	computed: {
		filterCoupon() {
			return function (couponItem) {
				// 单商品
				if (couponItem.plugsCoupons.goodsType == 2) {
					let avaliableGoodsList = this.orderData.filter((goodsItem) => {
						let ids = couponItem.plugsCoupons.goodsIds;
						let goodsId = goodsItem.goodsId;
						return ids.includes(goodsItem.goodsId + '');
					});
					let totalMoney = 0;
					avaliableGoodsList.forEach((item) => {
						if (item.goodsSpecification.length > 0) {
							totalMoney += item.goodsSpecification[0].dealPrice * item.goodsNum;
						} else {
							totalMoney += item.plugsGoods.goodsPrice * item.goodsNum;
						}
					});
					if (totalMoney >= couponItem.plugsCoupons.howMuchFullMoney) {
						return 1;
					} else {
						return 0;
					}
					// 多商品
				} else if (couponItem.plugsCoupons.goodsType == 1) {
					if (this.priceTotal >= couponItem.plugsCoupons.howMuchFullMoney) {
						return 1;
					} else {
						return 0;
					}
				}
			};
		}
	},
	methods: {
		// 进来获取默认收货地址
		getuserInfo() {
			let data = {
				userId: uni.getStorageSync('userInfo').id
			};
			searchUserInfoById(data).then((res) => {
				if (res.code == 200) {
					this.userInfo = res.body;
					if (res.body.mobile == 0) {
						this.thataddress.mobile = '';
					} else {
						this.thataddress.mobile = this.userInfo.mobile;
					}

					if (
						res.body.receiveAddressJson != '' &&
						res.body.receiveAddressJson != null &&
						res.body.receiveAddressJson != '[]'
					) {
						this.addressList = JSON.parse(res.body.receiveAddressJson);
						this.addressList.forEach((item) => {
							if (item.default) {
								this.addressData = item;
							}
						});
						console.log(this.addressData, 222222);
					}
				}
			});
		},

		// 获取优惠券
		getcouponList() {
			let postdata = {
				businessId: this.nowBusinessInfo.businessId.toString(),
				status: 0
			};

			searchPlugsCouponsByUserId(postdata).then((res) => {
				if (res.code == 200) {
					let arr1 = [];
					let arr2 = [];
					arr1 = res.body.list.filter((itm) => itm.couponType !== 2);
					console.log(arr1, 'arr1');

					arr1.forEach((item) => {
						if (item.plugsCoupons.goodsType == 1) {
							arr2.push(item);
						} else {
							this.orderData.forEach((element) => {
								if (item.plugsCoupons.goodsIds.includes(element.goodsId + '')) {
									arr2.push(item);
								}
							});
						}
					});

					let map = new Map();
					for (let item of arr2) {
						if (!map.has(item.id)) {
							map.set(item.id, item);
						}
					}
					this.couponList = [...map.values()];
				}
			});
		},

		// 计算价格
		calcTotal() {
			let list = this.orderData;
			let nowBusin = uni.getStorageSync('nowBusinessInfo');
			let total = 0;
			this.orderData.forEach((item, index) => {
				let goodsTotal = 0;
				if (item.goodsSpecification.length > 0) {
					total += item.goodsSpecification[0].dealPrice * item.goodsNum;
					goodsTotal = item.goodsSpecification[0].dealPrice * item.goodsNum;
					this.$set(this.orderData[index], 'goodsTotal', goodsTotal);
				} else {
					total += item.plugsGoods.goodsPrice * item.goodsNum;
					goodsTotal = item.plugsGoods.goodsPrice * item.goodsNum;
					console.log(goodsTotal);
					this.$set(this.orderData[index], 'goodsTotal', goodsTotal);
				}
			});
			this.priceTotal = Number(total.toFixed(2));
			console.log(this.priceTotal, 'this.priceTotal');
			if (this.priceTotal >= Number(nowBusin.shopExpressFee.freeShippingNum)) {
				this.distributionFee = 0;
			} else {
				this.distributionFee = Number(nowBusin.shopExpressFee.allShopExpressFee);
			}
			console.log(this.distributionFee, 'this.distributionFee');
			this.zongPrice = (Number(this.priceTotal) + Number(this.distributionFee)).toFixed(2);
			console.log(this.zongPrice, 'this.zongPrice');
		},
		//显示优惠券面板
		toggleMask(type) {
			let timer = type === 'show' ? 10 : 300;
			let state = type === 'show' ? 1 : 0;
			this.maskState = 2;
			setTimeout(() => {
				this.maskState = state;
			}, timer);
		},

		useCoupon(item, index) {
			if (!item.checked) {
				this.couponList.forEach((item) => {
					item.checked = false;
				});
				this.selectCoupon = item;
				this.maskState = 0;
				if (this.selectCoupon.couponType == 0 || this.selectCoupon.couponType == 1) {
					this.zongPrice = Number(
						this.priceTotal + this.distributionFee - this.selectCoupon.plugsCoupons.couponDenomination
					).toFixed(2);
					this.couponPrice = Number(this.priceTotal + this.distributionFee - this.zongPrice).toFixed(2);
				} else if (this.selectCoupon.couponType == 3) {
					this.zongPrice = Number(
						(this.priceTotal + this.distributionFee) * this.selectCoupon.plugsCoupons.couponDenomination
					).toFixed(2);
					this.couponPrice = Number(this.priceTotal + this.distributionFee - this.zongPrice).toFixed(2);
				}
				item.checked = !item.checked;
			} else {
				this.selectCoupon = {};
				this.maskState = 0;
				this.zongPrice = (Number(this.priceTotal) + Number(this.distributionFee)).toFixed(2);
				this.couponList.forEach((item) => {
					item.checked = false;
				});
			}
		},

		getNowTime() {
			let date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			let hour = date.getHours();
			let minute = date.getMinutes();
			let second = date.getSeconds();
			let time =
				year +
				'-' +
				this.addZero(month) +
				'-' +
				this.addZero(day) +
				' ' +
				this.addZero(hour) +
				':' +
				this.addZero(minute) +
				':' +
				this.addZero(second);
			return time;
		},
		addZero(s) {
			return s < 10 ? '0' + s : s;
		},
		getPhone(phone) {
			uni.makePhoneCall({
				phoneNumber: phone //仅为示例
			});
		},
		openMap(lon, lat) {
			uni.getLocation({
				success: (res) => {
					// res.latitude=lat;
					// res.longitude=lon;
					// console.log('location success', parseFloat(lat), parseFloat(lon))
					uni.openLocation({
						latitude: parseFloat(lat),
						longitude: parseFloat(lon),
						scale: 18
					});
				}
			});
		},

		selectOrderTap(e) {
			this.logisticsType = e;
		},

		space(lat1, lng1, lat2, lng2) {
			var radLat1 = (lat1 * Math.PI) / 180.0;
			var radLat2 = (lat2 * Math.PI) / 180.0;
			var a = radLat1 - radLat2;
			var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
			var s =
				2 *
				Math.asin(
					Math.sqrt(
						Math.pow(Math.sin(a / 2), 2) +
							Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
					)
				);
			s = s * 6378.137;
			s = Math.round(s * 10000) / 10000;
			return s; // 单位千米
		},

		paypalOrderTap(e) {
			uni.showLoading({
				title: '订单准备中',
				mask: true
			});
			if (this.nowBusinessInfo.logisticsType.noDelivery == 0) {
				if (this.logisticsType == 3) {
					if (!this.thataddress.mobile) {
						uni.showModal({
							title: '订单提示',
							content: '请输入联系电话',
							success(res) {}
						});
						uni.hideLoading();
						return;
					} else if (!/^1[3456789]\d{9}$/.test(this.thataddress.mobile)) {
						uni.showModal({
							title: '手机格式错误',
							content: '请输入正确的手机号码',
							success(res) {}
						});
						uni.hideLoading();
						return;
					}
				} else if (this.logisticsType == 1 || this.logisticsType == 2) {
					if (JSON.stringify(this.addressData) == '{}') {
						uni.showModal({
							title: '订单提示',
							content: '还没有收货地址，请先选择或添加收货地址！',
							confirmText: '选择地址',
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/packageA/address/addressList?type=select'
									});
								}
							}
						});
						uni.hideLoading();
						return;
					}
				}
			}

			let postData = {
				latitude: this.addressData.latitude || 0,
				logisticsType: this.logisticsType,
				longitude: this.addressData.longitude || 0,
				payMoney: this.zongPrice,
				businessId: this.nowBusinessInfo.businessId.toString(),
				orderGoodsDTOS: this.orderList,
				receiverAddress:
					this.logisticsType == 2 ? JSON.stringify(this.addressData) : JSON.stringify(this.thataddress),
				shopCartIds: this.cartIdList,
				userCouponId: JSON.stringify(this.selectCoupon) !== '{}' ? this.selectCoupon.id : 0,
				orderRemark: this.ordermark,
				areaCode: this.addressData.areaCode || 0,
				expectPickTime: this.logisticsType == 3 ? this.expectPickTime : ''
			};

			createOrder(postData)
				.then((res) => {
					uni.hideLoading();
					if (res.code === 200) {
						uni.showToast({
							icon: 'success',
							title: '创建成功'
						});

						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/order/pay?info=${encodeURIComponent(JSON.stringify(res.body))}`
							});
						}, 800);
					} else {
						uni.showModal({
							title: '订单提示',
							content: res.msg,
							showCancel: false
						});
						return;
					}
				})
				.catch((err) => {
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						title: '创建失败，请稍后再试'
					});
				});
			console.log(postData);
		},

		paypalOrderTapIntegral() {
			let that = this;
			uni.showModal({
				title: '提示',
				content: '您确定要兑换吗?',
				success(res) {
					if (res.confirm) {
						console.log(that.goodsType);
						if (that.goodsType == 1) {
							if (that.userInfo.integral < that.priceTotal) {
								uni.showToast({
									title: '积分不足',
									icon: 'none'
								});
								return;
							}
						}

						uni.showLoading({
							title: '订单准备中',
							mask: true
						});
						if (that.nowBusinessInfo.logisticsType.noDelivery == 0) {
							if (that.logisticsType == 3) {
								if (!that.thataddress.mobile) {
									uni.showModal({
										title: '订单提示',
										content: '请输入联系电话',
										success(res) {}
									});
									uni.hideLoading();
									return;
								} else if (!/^1[3456789]\d{9}$/.test(that.thataddress.mobile)) {
									uni.showModal({
										title: '手机格式错误',
										content: '请输入正确的手机号码',
										success(res) {}
									});
									uni.hideLoading();
									return;
								}
							} else if (that.logisticsType == 1 || that.logisticsType == 2) {
								if (!that.addressData) {
									uni.showModal({
										title: '订单提示',
										content: '还没有收货地址，请先选择或添加收货地址！',
										confirmText: '选择地址',
										success(res) {
											if (res.confirm) {
												console.log(123);
												uni.navigateTo({
													url: '/packageA/address/addressList?type=select'
												});
											}
										}
									});
									uni.hideLoading();
									return;
								}
							}
						}
						let postData = {
							latitude: that.addressData.latitude || 0,
							logisticsType: that.nowBusinessInfo.logisticsType.noDelivery == 0 ? that.logisticsType : 3,
							longitude: that.addressData.longitude || 0,
							payMoney: that.zongPrice,
							businessId: that.nowBusinessInfo.businessId.toString(),
							orderGoodsDTOS: that.orderList,
							receiverAddress:
								that.logisticsType == 1
									? JSON.stringify(that.addressData)
									: JSON.stringify(that.thataddress),
							shopCartIds: that.cartIdList,
							userCouponId: JSON.stringify(that.selectCoupon) !== '{}' ? that.selectCoupon.id : 0,
							orderRemark: that.ordermark,
							areaCode: that.addressData.areaCode || 0,
							expectPickTime: that.logisticsType == 3 ? that.expectPickTime : ''
						};
						createOrder(postData)
							.then((res) => {
								uni.hideLoading();
								if (res.code === 200) {
									uni.showToast({
										icon: 'success',
										title: '创建成功'
									});
									that.integralInfo = res.body;
									that.scorePay();
								} else {
									uni.showModal({
										title: '订单提示',
										content: res.msg,
										showCancel: false
									});
									return;
								}
							})
							.catch((err) => {
								uni.hideLoading();
								uni.showToast({
									icon: 'success',
									title: '创建失败，请稍后再试'
								});
							});
					} else if (res.cancel) {
					}
				}
			});
		},

		// 积分支付
		scorePay() {
			let that = this;
			let postdata = {
				client: '小程序',
				extParm: '',
				openId: uni.getStorageSync('openId'),
				orderId: that.integralInfo.id,
				orderNo: that.integralInfo.orderNo,
				payMoney: that.integralInfo.payMoney,
				payType: 0,
				payWay: 3,
				businessId: that.nowBusinessInfo.businessId.toString()
			};
			uni.showLoading({
				title: '支付准备中',
				mask: true
			});
			integralOrderPay(postdata).then((res) => {
				if (res.code == 200) {
					uni.hideLoading();
					uni.showToast({
						title: '积分支付成功'
					});
					setTimeout(function () {
						uni.redirectTo({
							url: '/pages/order/paysuccess'
						});
					}, 600);
				} else {
					uni.hideLoading();
					uni.showToast({
						title: res.msg
					});
				}
			});
		},

		// 余额支付
		balancePay() {
			let that = this;
			if (that.balanceMoney < that.orderInfo.payMoney) {
				uni.showModal({
					title: '提示',
					content: '余额不足,请充值',
					success: function (res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/index/membercz'
							});
						} else if (res.cancel) {
						}
					}
				});
			}
			let postdata = {
				client: '小程序',
				extParm: '',
				openId: that.openId,
				orderId: that.orderInfo.id,
				orderNo: that.orderInfo.orderNo,
				payMoney: that.orderInfo.payMoney,
				payType: 0,
				payWay: 4
			};
			uni.showLoading({
				title: '支付准备中',
				mask: true
			});
			that.Post2('/api/pay/balanceOrderPay', postdata).then((res) => {
				if (res.code == 200) {
					uni.hideLoading();
					uni.showToast({
						title: '微信支付成功'
					});
					setTimeout(function () {
						let id = JSON.parse(JSON.stringify(that.orderInfo.id));
						uni.navigateTo({
							url: `/pages/order/paysuccess?id=${id}&&money=${that.orderInfo.payMoney}`
						});
					}, 600);
				} else {
					uni.hideLoading();
					uni.showToast({
						title: res.msg
					});
				}
			});
		},
		getBindWechatPhoneTap(e) {
			let wechatServerJSON = {
				code: e.detail.code
			};
			let data = {
				wechatServerJSON: JSON.stringify(wechatServerJSON)
			};
			getWeChartUserPhone(data).then((res) => {
				if (res.code == 200) {
					console.log(res.body);
					this.thataddress.mobile = res.body.phone_info.purePhoneNumber;
					updateUserInfo({ mobile: this.thataddress.mobile }).then((ues) => {
						if (ues.code == 200) {
						}
					});
				}
			});
		},
		getPathTap(path) {
			uni.navigateTo({
				url: path
			});
		}
	}
};
</script>

<style lang="less">
page {
	background-color: #f5f5f5;
}
.content {
	padding: 20rpx;
	padding-bottom: 120rpx;
}

.menu_box {
	background-color: #fff;
	padding: 30rpx;
	border-radius: 20rpx;
	margin-bottom: 20rpx;
}

// 物流方式
.delivery_box {
	display: flex;
	/deep/.u-radio {
		flex: 1;
	}
}
// 物流地址
.address_box {
	.address {
		display: flex;
		justify-content: space-between;
		.icone {
			margin-right: 10rpx;
		}
		.left {
			flex: 1;
			min-width: 0;
			.txt {
				font-size: 30rpx;
				color: #333;
				// white-space: nowrap;
				// overflow: hidden;
				// text-overflow: ellipsis;
				font-weight: 500;
			}
			.tit {
				padding-top: 10rpx;
				color: #1c1c1c;
				font-size: 26rpx;
				color: #999;
				span {
					padding-left: 20rpx;
				}
			}
		}

		.right {
			width: 80rpx;
			display: flex;
			align-items: center;
			justify-content: flex-end;
		}
	}

	.none_address {
		width: 100%;
		height: 150rpx;
		border: 2px dashed #e5e5e5;
		border-radius: 10rpx;
		color: #bbb;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		span {
			font-size: 26rpx;
		}
	}
}

// 自提信息
.phone_box {
	display: flex;
	justify-content: space-between;

	.left {
		width: 70rpx;
	}
	.input-box {
		flex: 1;
		display: flex;
		align-items: center;
		.input {
			height: 60rpx;
			border: 1px solid #eee;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			input {
				width: 224rpx;
				color: #1c1c1c;
				font-size: 30rpx;
				height: 100%;
			}
		}
	}

	.btn {
		color: #fff;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 20rpx;
		font-size: 22rpx;
		position: relative;

		button {
			position: absolute;
			width: 100%;
			height: 100%;
			top: 0;
			left: 0;
			opacity: 0;
		}
	}

	.thataddress {
		.title {
			font-size: 28rpx;
			font-weight: 500;
		}
		.name {
			color: #9e9e9e;
		}
	}
}
.line {
	margin: 20rpx 0;
	height: 1rpx;
	background-color: #f5f5f5;
}

// 商品信息
.goods {
	display: flex;
	margin-bottom: 20rpx;
	.left {
		width: 130rpx;
		height: 130rpx;
		background-color: #f8f9fa;
		border-radius: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 20rpx;
		}
	}
	.right {
		flex: 1;
		box-sizing: border-box;
		padding-left: 24rpx;
		overflow: hidden;
		.title {
			font-size: 30rpx;
			color: #303133;
			display: -webkit-box; //将对象作为弹性伸缩盒子模型显示
			-webkit-box-orient: vertical; //设置或检索伸缩盒对象的子元素的排列方式
			-webkit-line-clamp: 1; //一个块元素显示的文本的行数
			overflow: hidden; //溢出隐藏
		}
		.guige {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #333;
		}
		.spec {
			font-size: 26rpx;
			color: #909399;
			margin: 10px 0;
		}
		.price-box {
			display: flex;
			align-items: center;
			color: #303133;
			padding-top: 10rpx;
			font-size: 26rpx;
			.price {
				color: #f85e52;
			}
			.number {
				font-size: 26rpx;
				color: #606266;
				margin-left: 20rpx;
			}
		}
	}

	.step-box {
		position: relative;
	}
}

.order_info_box {
	.label-coupon {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 30rpx;
		color: #333;
	}
	.label {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 30rpx;
		height: 90rpx;
		color: #333;
	}
}

.footer {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	z-index: 100;
	background-color: #fff;
	padding-bottom: env(safe-area-inset-bottom);
	border-top-left-radius: 20rpx;
	border-top-right-radius: 20rpx;

	.label {
		display: flex;
		align-items: center;
		height: 120rpx;
		width: 100%;
		justify-content: flex-end;

		.tex {
			font-size: 30rpx;
			margin: 0 10rpx;
		}
		.button {
			width: 200rpx;
			height: 80rpx;
			border-radius: 40rpx;
			color: #fff;
			font-size: 32rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #f54319;
			margin: 0 30rpx;
		}
	}
}

/* 优惠券面板 */
.mask {
	display: flex;
	align-items: flex-end;
	position: fixed;
	left: 0;
	top: var(--window-top);
	bottom: 0;
	width: 100%;
	background: rgba(0, 0, 0, 0);
	z-index: 9995;
	transition: 0.3s;

	.mask-content {
		width: 100%;
		min-height: 30vh;
		max-height: 70vh;
		background: #f3f3f3;
		transform: translateY(100%);
		transition: 0.3s;
		overflow-y: scroll;
	}

	&.none {
		display: none;
	}

	&.show {
		background: rgba(0, 0, 0, 0.4);

		.mask-content {
			transform: translateY(0);
		}
	}
}

/* 优惠券列表 */
.coupon-item {
	display: flex;
	flex-direction: column;
	margin: 20rpx 24rpx;
	background: #fff;

	.con {
		display: flex;
		align-items: center;
		position: relative;
		height: 120rpx;
		padding: 0 30rpx;

		&:after {
			position: absolute;
			left: 0;
			bottom: 0;
			content: '';
			width: 100%;
			height: 0;
			border-bottom: 1px dashed #f3f3f3;
			transform: scaleY(50%);
		}
	}

	.left {
		display: flex;
		flex-direction: column;
		justify-content: center;
		flex: 1;
		overflow: hidden;
		height: 100rpx;
	}

	.title {
		font-size: 32rpx;
		color: #303133;
		margin-bottom: 10rpx;
	}

	.time {
		font-size: 24rpx;
		color: #909399;
	}

	.right {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 26rpx;
		color: #606266;
		height: 100rpx;
	}

	.price {
		font-size: 44rpx;
		color: #ff633d;

		&:before {
			font-size: 34rpx;
		}
	}

	.getlin {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx;

		.tips {
			font-size: 24rpx;
			color: #999;
			line-height: 60rpx;
		}

		.linqu {
			font-size: 26rpx;
			color: #fff;
			background-color: orangered;
			width: 100rpx;
			height: 40rpx;
			border-radius: 20rpx;
			text-align: center;
			line-height: 40rpx;
		}
	}

	.circle {
		position: absolute;
		left: -6rpx;
		bottom: -10rpx;
		z-index: 10;
		width: 20rpx;
		height: 20rpx;
		background: #f3f3f3;
		border-radius: 100px;

		&.r {
			left: auto;
			right: -6rpx;
		}
	}
}
</style>
